<template>
	<view class="container">
		<view class="wrap" v-if='showPage'>
			<!-- 商品轮播图 和 商品名称 -->
			<view class="swiper-wrap" v-if='swiperList.length > 0'>
				<swiper :current='currentIndex' @change='changeImg' autoplay="true" duration="1000" interval="3500"
					circular>
					<swiper-item v-for='(item,index) in swiperList'>
						<view class="img">
							<base-img :src='item'></base-img>
						</view>
						<view class="dots-wrap line_center" style="z-index: 999;">{{index + 1}}/{{swiperList.length}}
						</view>
					</swiper-item>
				</swiper>
			</view>
			<view class="goods-info-wrap">
				<view class="goods-price-wrap" v-if='goodsType == 1'>
					<view class="left-goods-price">
						<text class="fuhao">￥</text>
						<text class="price">{{goodsDetail.price}}</text>
						<text
							style="color: #ccc;font-size: 24rpx;margin-left: 5rpx;text-decoration: line-through;">￥{{goodsDetail.tag_price}}</text>
					</view>
					<view class="share-wrap" @tap='getPoster(goodsDetail.g_id)' v-if='login_status == 0'>
						<view class="share-icon">
							<image src='../img/share.png'></image>
						</view>
					</view>
				</view>
				<view class="goods-name-wrap">
					{{goodsDetail.goodname}}
				</view>
			</view>
			<!-- 商品简介 -->
			<view class="goods-detail">
				<text class="tit">商品详情</text>
				<view class="goods-detai-con">
					<rich-text :nodes="goodsDetail.detail"></rich-text>
				</view>
			</view>
			<view class="footer">
				<view class="footer-wrap" v-if='goodsType == 1'>
					<view class="no-stock-btn line_center" v-if="goodsDetail.stock == 0">库存不足</view>
					<block v-else>
						<view class="cart-btn add-cart line_center" @tap="handleGoodsBtn(1)">加入购物车</view>
						<view class="cart-btn buy line_center" @tap="handleGoodsBtn(2)">立即购买</view>
					</block>
				</view>
			</view>
		</view>
		<button class="kefu" open-type="contact">
			<image src='/static/kefu.png'></image>
		</button>
		<!-- 商品购买弹窗 -->
		<modal :showModal='showGoodsModal'>
			<buy-modal :goodsDetail='goodsDetail' :showBuyModal='showGoodsModal' :btnType='btnType'
				@close='close'></buy-modal>
		</modal>
		<!-- 生成海报 -->
		<modal :showModal='showPosterModal'>
			<poster @close='close'></poster>
		</modal>
		<!-- 购买须知 -->
		<modal :showModal="showLoginModal">
			<wx-login :showLoginModal='showLoginModal' @onsubmits='onsubmits(2)' @close='closeLoginModal'></wx-login>
		</modal>
	</view>
</template>

<script>
	const app = getApp();
	import wxLogin from '@/components/wx-login/pay-about.vue';
	import baseImg from '@/components/base-img/base-img.vue';
	import modal from '@/components/modal/modal.vue';
	import buyModal from '@/components/buy-modal/buy-modal.vue';
	import poster from '@/components/poster/poster.vue';
	export default {
		components: {
			buyModal,
			modal,
			baseImg,
			poster,
			wxLogin
		},
		data() {
			return {
				showPage: false,
				showGoodsModal: false,
				showPosterModal: false,
				showLoginModal: false,
				btnType: 0,
				goodsType: 1,
				goodsId: '',
				goodsDetail: {},
				swiperList: [],
				currentIndex: 0,
				login_status: 1,
				uId: '',
			};
		},
		onShow(){
			this.uId = uni.getStorageSync('UID') ? uni.getStorageSync('UID') : "";
		},
		onLoad(option) {
			if (option.scene) {
				var arr = []
				arr = decodeURIComponent(option.scene).replace(/_/ig, ",").split(',')
				console.log(arr)
				this.goodsId = arr[0]
				getApp().globalData.parent_sn = arr[1];
				console.log('getApp().globalData.parent_sn', getApp().globalData.parent_sn)
			} else {
				console.log('分享链接进来 === 》', option)
				if (option.uid) app.globalData.parent_sn = option.uid;
				if (option.type) this.goodsType = 1;
				if (option.goods_id) this.goodsId = option.goods_id;
			}

			this.getGoodsDetail()
		},
		onShareAppMessage(res) {
			let path, title, imageUrl;
			path =
				`/pages/goods-page/goods-detail/goods-detail?goods_id=${this.goodsId}&type=1&uid=${this.uId}`;
			title = this.goodsDetail.goodname;
			imageUrl = this.goodsDetail.pic;
			console.log('path', path)
			return {
				path,
				title,
				imageUrl
			}
		},
		onShareTimeline() {
			return {
				title: '智诚智谷',
				query: `goods_id=${this.goodsId}&type=1&uid=${this.uId}`
			};
		},
		onHide() {
			this.close()
		},
		methods: {
			// 扫码绑定下级
			bind_fuid() {
				let that = this;
				app.globalRequest({
					api: app.getApi().home.bindUfid,
					data: {
						share_code: getApp().globalData.parent_sn
					}
				}).then(res => {
					if (res.code == 0) {

					}
				}).catch(err => {

				})
			},
			/* 获取商品详情数据 */
			getGoodsDetail() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				app.globalRequest({
					api: app.getApi().goodsdata.goodsDetail,
					data: {
						g_id: this.goodsId,
						token: uni.getStorageSync('token')
					}
				}).then(res => {
					that.showPage = true;
					if (res.code == 1) {
						let goodsDetail = res.data;
						if (goodsDetail.rotograph) {
							that.swiperList = goodsDetail.rotograph.split(',');
						} else {
							that.swiperList = []
						}
						goodsDetail.detail = goodsDetail.detail ? goodsDetail.detail.replace(/<img/gi,
							"<img class='richImg' style='width:100%'") : '';
						goodsDetail.num = 1;
						that.goodsDetail = res.data;
						console.log('that.goodsDetail', that.goodsDetail)
						that.getIsLogin()
					}
				}).catch(err => {

				})
			},
			changeImg(e) {
				if (e.detail.source == 'touch') {
					this.currentIndex = e.detail.current;
				}
			},
			//判断登录状态
			getIsLogin() {
				app.globalRequest({
					api: app.getApi().public.islogin,
					method: 'post'
				}).then(res => {
					if (res.code == 1) {
						this.login_status = res.data.is_not_login
					}
				}).catch(err => {

				})
			},
			getPoster(id) {
				let that = this
				// 获取登录状态
				app.globalRequest({
					api: app.getApi().public.islogin,
					method: 'post'
				}).then(res => {
					if (res.code == 1) {
						that.login_status = res.data.is_not_login
						if (that.login_status == 1) {
							uni.showToast({
								title: '请授权登录',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/user/login/login_auth',
								})
							}, 1000)
							return
						} else {
							app.globalRequest({
								api: app.getApi().mall.shareGoods,
								method: 'post',
								data: {
									g_id: id
								}
							}).then(res => {
								if (res.code == 1) {
									this.showPosterModal = true;
									uni.$emit('shareUrl', res.data)
								}
							}).catch(err => {

							})
						}
					}
				}).catch(err => {

				})
			},
			handleGoodsBtn(type) {
				if (this.goodsDetail.id_dividend == 1) {
					this.showLoginModal = true;

				} else {
					this.btnType = type;
					this.showGoodsModal = true;
				}

				// this.btnType = type;
				// this.showGoodsModal = true;
			},
			clickEv() {
				console.log('1111111111111')
				this.showGoodsModal = true;
			},
			close() {
				this.showGoodsModal = false;
				this.showPosterModal = false
				this.showLoginModal = false
			},
			closeLoginModal() {
				this.showLoginModal = false
			},
			onsubmits(type) {
				this.showLoginModal = false
				this.btnType = type;
				this.showGoodsModal = true;
			}
		}
	}
</script>
<style>
	.richImg {
		width: 100% !important;
		height: auto !important;
		display: block !important;
	}
</style>
<style lang="scss" scoped>
	image {
		width: 100%;
		height: 100%;
	}

	.wrap {}

	.goods-info-wrap {
		background: $dis-con-color;
		padding: 16rpx 16rpx 26rpx;
		margin: 20rpx 24rpx 0;
		border-radius: 8rpx;
	}

	.rich-img {
		max-width: 100% !important;
		width: 100% !important;
		height: auto !important;
		display: block
	}

	.swiper-wrap {
		width: 100%;
		height: 750rpx;
		position: relative;

		swiper {
			width: 100%;
			height: 100%;

			swiper-item,
			.img {
				width: 100%;
				height: 100%;
			}
		}

		.dots-wrap {
			width: 84rpx;
			height: 40rpx;
			background: rgba(255, 255, 255, 0.67);
			border-radius: 39rpx;
			position: absolute;
			right: 24rpx;
			bottom: 24rpx;
			font-size: 20rpx;
		}
	}

	.goods-price-wrap {
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;

		.left-goods-price {
			display: flex;
			align-items: baseline;
			color: $dis-font-color;
			width: 85%;

			.fuhao {
				font-size: 28rpx;
			}

			.price {
				font-size: 50rpx;
			}
		}

		.share-wrap {
			display: flex;
			align-items: center;

			.share-icon {
				width: 48rpx;
				height: 48rpx;
				margin-right: 14rpx;
			}
		}
	}

	.goods-name-wrap {
		margin-top: 8rpx;
		box-sizing: border-box;
		color: #333333;
		font-size: 32rpx;
	}

	.goods-detail {
		margin-top: 20rpx;
		padding: 32rpx 24rpx 200rpx;

		.tit {
			font-size: 32rpx;
			color: #313131;
		}

		.goods-detai-con {
			min-height: 600rpx;
			margin-top: 40rpx;
		}
	}

	.footer {
		width: 100%;
		height: 100rpx;
		background: $dis-con-color;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;

		.footer-wrap {
			width: 100%;
			height: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			color: #fff;
			padding: 0 40rpx;
			box-sizing: border-box;
		}

		.no-stock-btn {
			width: 100%;
			height: 84rpx;
			background: linear-gradient(82deg, #ececec 0%, #d3d3d3 100%);
			color: #999;
			border-radius: 42rpx;
		}

		.cart-btn {
			width: 328rpx;
			height: 84rpx;
			border-radius: 42rpx;
			font-size: 32rpx;
		}

		.add-cart {
			background: #FFCD02;
		}

		.buy {
			background: $dis-liner-color;
		}
	}

	.kefu {
		width: 48rpx;
		height: 48rpx;
		position: fixed;
		right: 38rpx;
		bottom: 200rpx;
		z-index: 222;
	}
</style>